package bibliotheque.wave;

import java.util.ArrayList;
import java.util.concurrent.Semaphore;

public class Wave {
    
    public enum State {RUNNING, LOCALY_ENDED, GLOBALY_ENDED};
    private State state;
    private WaveId id;
    private WaveAgentRemote up;
    private ArrayList<WaveAgentRemote> waits;
    public Semaphore mutex;
    public Semaphore first_mutex;
    
    public Wave(WaveId id, WaveAgentRemote up, ArrayList<WaveAgentRemote> waits) {
        this.id = id;
        this.up = up;
        this.waits = waits;
        this.state = State.RUNNING;
        this.mutex = new Semaphore(1);
        this.first_mutex = new Semaphore(0);
    }
    
    public void Acknowledged(WaveAgentRemote agent) {
        this.waits.remove(agent);
    }
    
    public State GetState() {
        if(this.up == null && this.waits.size() == 0) this.state = State.GLOBALY_ENDED;
        else if(this.waits.size() == 0) this.state = State.LOCALY_ENDED;
        else this.state = State.RUNNING;
        return(this.state);
    }
    
    public WaveAgentRemote GetUp() {
        return(this.up);
    }
}
